﻿/*
 *    Copyright [2010] [Daniel Sawan]
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */

package com.kikoulol.jarmory.extractor;

import java.util.ArrayList;
import java.util.List;

import com.kikoulol.jarmory.extractor.enums.Regions;
import com.kikoulol.jarmory.extractor.enums.LadderTypes;
import com.kikoulol.jarmory.extractor.xml.TeamInfo;
import com.kikoulol.jarmory.extractor.xml.ArenaLadder;

public class Ladder
{
  private Regions region;
  private String battleGroupName;
  private LadderTypes ladderType;

  private int teamPerView = 20; // default wow armory value

  public Ladder(Regions region, String battleGroupName, LadderTypes ladderType)
  {
    this.region = region;

    if (battleGroupName.contains("b="))
    {
      this.battleGroupName = battleGroupName.replace("b=", "");
    }
    else
    {
      this.battleGroupName = battleGroupName;
    }
    this.ladderType = ladderType;
  }

  public ArenaLadder getLadderPage(int pageNumber)
  {
    return new ArenaLadder(this.region, this.battleGroupName, this.ladderType, pageNumber);
  }

  public List<TeamInfo> getTeams(long numberOfTeams)
  {
    // First pass to get the maxPage number
    ArenaLadder ladderPage = this.getLadderPage(1);
    int maxPage = Integer.parseInt(ladderPage.getMaxPage());

    List<TeamInfo> returnList = new ArrayList<TeamInfo>();
    int i = 1; // ladder page number
    while (returnList.size() < numberOfTeams && i <= maxPage)
    {
      // Load the ladder Page
      ladderPage = this.getLadderPage(i);
      // Add the actuel list of team to the memory
      List<TeamInfo> teams = ladderPage.getArenaTeams();
      for (TeamInfo team : teams)
      {
        // Team myTeam = new Team(armoryUrl, team.getUrl());
        returnList.add(team);
        if (returnList.size() == numberOfTeams)
        {
          break;
        }
      }
      i++; // go to the next armory ladder page
      ladderPage = this.getLadderPage(i);
    }
    return returnList;
  }

  public List<TeamInfo> getGladTeam()
  {
    ArenaLadder ladder = this.getLadderPage(getMaxPage());
    List<TeamInfo> teams = ladder.getArenaTeams();
    TeamInfo lastTeam = teams.get(teams.size() - 1);
    double gladNum = (5.0 / 100.0) * Double.parseDouble(lastTeam.getRanking());
    String gladNumber = "" + gladNum;
    int test = Integer.parseInt(gladNumber.substring(0, gladNumber.indexOf(".")));
    return getTeams(test);
  }

  public List<TeamInfo> getTeams()
  {
    return getTeams(this.getMaxPage());
  }

  public int getMaxPage()
  {
    ArenaLadder ladder = this.getLadderPage(1);
    return Integer.parseInt(ladder.getMaxPage());
  }

  public int getTeamPerView()
  {
    return teamPerView;
  }

  public void setTeamPerView(int teamPerView)
  {
    this.teamPerView = teamPerView;
  }

}
